package com.yra.controller.manager;

import com.yra.domain.Category;
import com.yra.service.CategoryService;
import com.yra.util.PageQueryUtil;
import com.yra.util.Result;
import com.yra.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.*;

/**
 * @Author: yrq
 * @Date: Create in 9:10 2019/12/2
 */
@Controller
@RequestMapping("/manager")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    @RequestMapping(value = "categories",method = RequestMethod.GET)
    public ModelAndView goToCategories(HttpServletRequest request, @RequestParam("categoryLevel") Byte categoryLevel,@RequestParam("parentId") Integer parentId){
        ModelAndView mv = new ModelAndView("/admin/newbee_mall_category");
        request.setAttribute("categoryLevel", categoryLevel);
        request.setAttribute("parentId",parentId);
        return mv;
    }

    @RequestMapping(value = "categories/list",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<Result> categoryList(@RequestParam Map<String, Object> params){
//        List<Category> categories = categoryService.categoryByLevel(categoryLevel);
//        System.out.println(categories.get(0));
        PageQueryUtil pageQueryUtil = new PageQueryUtil(params);
        return ResponseEntity.status(HttpStatus.OK).body(ResultGenerator.genSuccessResult(categoryService.categoryListPage(pageQueryUtil)));
    }

    @RequestMapping(value = "categories/save",method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<HttpStatus> categorySave(@RequestBody Category category){
        categoryService.categoryInsert(category);
        return ResponseEntity.status(HttpStatus.OK).body(HttpStatus.CREATED);
    }

    @RequestMapping(value = "categories/update",method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<HttpStatus> categoryUpdate(@RequestBody Category category){
//        category.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        categoryService.categoryUpdate(category);
        return ResponseEntity.status(HttpStatus.OK).body(HttpStatus.OK);
    }

    @RequestMapping(value = "categories/delete",method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Boolean> categoryDelete(@RequestBody Integer[] ids){
        categoryService.categoryDelete(ids[0]);
        return ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @RequestMapping(value = "categories/listForSelect",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<Map> listForSelect(@RequestParam("categoryId") int categoryId){
        Category category = categoryService.categoryListByCategoryId(categoryId);
        Map categoryResult = new HashMap(2);
        if(category.getCategoryLevel()==1){
            List<Category> secondLevelCategories = categoryService.categoryListByLevelAndParentId(2, category.getCategoryId());
            categoryResult.put("secondLevelCategories", secondLevelCategories);
            if(secondLevelCategories.size()>0){
                List<Category> thirdLevelCategories = categoryService.categoryListByLevelAndParentId(3, secondLevelCategories.get(0).getCategoryId());
                categoryResult.put("thirdLevelCategories", thirdLevelCategories);
            }else {
                List<Category> thirdLevelCategories=new ArrayList<>();
                categoryResult.put("thirdLevelCategories",thirdLevelCategories);
            }
        }else {
            List<Category> thirdLevelCategories = categoryService.categoryListByLevelAndParentId(3, category.getCategoryId());
            categoryResult.put("thirdLevelCategories", thirdLevelCategories);
        }
        return ResponseEntity.status(HttpStatus.OK).body(categoryResult);
    }
}
